package com.vict.controller.web.appversion;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vict.bean.appversion.ao.*;
import com.vict.bean.appversion.vo.AppVersionVO;
import com.vict.bean.appversion.vo.SelectNowVersionVO;
import com.vict.entity.AppVersion;
import com.vict.enums.app.Platform;
import com.vict.framework.bean.MyPageInfo;
import com.vict.framework.bean.R;
import com.vict.framework.utils.IdUtils;
import com.vict.framework.web.ApiPrePath;
import com.vict.mapper.AppVersionMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@ApiPrePath
@Api
@Slf4j
@RestController
@RequestMapping("/appVersion")
public class AppVersionController {
    private static final String SEAGGER_TAGS = "app版本";

    @Autowired
    private AppVersionMapper appVersionMapper;

    @ApiOperation(tags = SEAGGER_TAGS, value = "获取app配置", httpMethod = "POST")
    @PostMapping("/insertVersion")
    public R insertVersion(@RequestBody InsertVersionAO insertVersionAO) {
        insertVersionAO.check();

        AppVersion appVersionExist = appVersionMapper.selectOne(new LambdaQueryWrapper<AppVersion>()
                .eq(AppVersion::getPlatform, insertVersionAO.getPlatform())
                .eq(AppVersion::getVersionStr, insertVersionAO.getVersionStr())
        );
        if(appVersionExist != null){
            throw new RuntimeException("版本号已存在");
        }

        AppVersion appVersion = new AppVersion();
        appVersion.setId(IdUtils.snowflakeId());
        appVersion.setPlatform(insertVersionAO.getPlatform());

        appVersion.setVersionNum(AppVersion.convertVersionNum(insertVersionAO.getVersionStr()));

        appVersion.setVersionStr(insertVersionAO.getVersionStr().trim());
        appVersion.setReleaseTime(new Timestamp(System.currentTimeMillis()));
        appVersion.setInfo(insertVersionAO.getInfo());
        appVersion.setFileUrl(insertVersionAO.getFileUrl());
        appVersion.setWgtUrl(insertVersionAO.getWgtUrl());
        appVersion.setUpdateMethod(insertVersionAO.getUpdateMethod());
        appVersion.setIsForce(insertVersionAO.getIsForce());
        appVersion.setStatus(AppVersion.Status.test);

        appVersionMapper.insert(appVersion);

        return R.ok();
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "app配置列表", httpMethod = "POST")
    @PostMapping("/appVersionList")
    public R<MyPageInfo<AppVersionVO>> appVersionList(@RequestBody AppVersionListAO appVersionListAO){

        // 数据查询
        Platform platform = Optional.ofNullable(appVersionListAO).map(o -> o.getPlatform()).orElse(null);

        Page page = PageHelper.startPage(appVersionListAO.getPageNum(), appVersionListAO.getPageSize());

        appVersionMapper.selectMyList(platform);

        List<AppVersion> pages = Optional.ofNullable(page.getResult()).orElse(new ArrayList<>());

        MyPageInfo<AppVersionVO> myPageInfo = new MyPageInfo<AppVersionVO>(page);
        List<AppVersionVO> pageVOS = new ArrayList<AppVersionVO>(pages.size());
        myPageInfo.setList(pageVOS);

        for(AppVersion appVersion : pages){
            AppVersionVO vo = new AppVersionVO();
            vo.setAppVersion(appVersion);
            pageVOS.add(vo);
        }
        return R.ok(myPageInfo);
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "app最新版本", httpMethod = "POST")
    @PostMapping("/selectNowVersion")
    public R<SelectNowVersionVO> selectNowVersion(){
        AppVersion android = appVersionMapper.selectVersion(Platform.Android);
        AppVersion ios = appVersionMapper.selectVersion(Platform.IOS);

        AppVersionVO androidAppVersionVO = new AppVersionVO();
        androidAppVersionVO.setAppVersion(android);

        AppVersionVO iosAppVersionVO = new AppVersionVO();
        iosAppVersionVO.setAppVersion(ios);

        SelectNowVersionVO selectNowVersionVO = new SelectNowVersionVO();
        selectNowVersionVO.setAndroid(androidAppVersionVO);
        selectNowVersionVO.setIos(iosAppVersionVO);

        return R.ok(selectNowVersionVO);
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "app删除", httpMethod = "POST")
    @PostMapping("/deleteVersion")
    public R appVersionList(@RequestBody DeleteVersionAO deleteVersionAO){
        deleteVersionAO.check();
        appVersionMapper.deleteById(deleteVersionAO.getId());
        return R.ok();
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "app修改", httpMethod = "POST")
    @PostMapping("/updateVersion")
    public R updateVersion(@RequestBody UpdateVersionAO updateVersionAO){
        updateVersionAO.check();

        Long id = updateVersionAO.getId();
        AppVersion appVersion = appVersionMapper.selectById(id);
        appVersion.setInfo(updateVersionAO.getInfo());
        appVersion.setFileUrl(updateVersionAO.getFileUrl());
        appVersion.setWgtUrl(updateVersionAO.getWgtUrl());
        appVersion.setUpdateMethod(updateVersionAO.getUpdateMethod());
        appVersion.setIsForce(updateVersionAO.getIsForce());
        appVersionMapper.updateById(appVersion);

        return R.ok();
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "app发布", httpMethod = "POST")
    @PostMapping("/releaseVersion")
    public R releaseVersion(@RequestBody ReleaseVersionAO releaseVersionAO){
        releaseVersionAO.check();

        AppVersion appVersion = appVersionMapper.selectById(releaseVersionAO.getId());

        appVersion.setStatus(AppVersion.Status.release);

        appVersionMapper.updateById(appVersion);

        return R.ok();
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "app撤回发布", httpMethod = "POST")
    @PostMapping("/callbackVersion")
    public R callbackVersion(@RequestBody CallbackVersionAO callbackVersionAO){
        callbackVersionAO.check();

        AppVersion appVersion = appVersionMapper.selectById(callbackVersionAO.getId());

        appVersion.setStatus(AppVersion.Status.test);

        appVersionMapper.updateById(appVersion);

        return R.ok();
    }


}
